/*
 * results.cc
 *
 *  Created on: Sep 17, 2012
 *      Author: shaohong
 */

#include "results.hh"

using namespace std;

std::istream& readResults(std::istream& is,
		std::back_insert_iterator<std::vector<Result>> insertIt) {
	struct Result newResult;

	while(!is.eof()) {
		is >> newResult.studentId ;
		if (is.eof()) {
			return is;
		}

		is >> newResult.ex1 >> newResult.ex2 >> newResult.ex3
				>> newResult.grade;

		insertIt = newResult;
	}
	return is;
}

void printResult(std::ostream& os, std::vector<Result>::const_iterator result) {

	os << result->studentId << " " << result->ex1 << " " << result->ex2 << " "
			<< result->ex3 << " " << result->grade << endl;
}

void printResults(std::ostream& os, std::vector<Result>::const_iterator begin,
		std::vector<Result>::const_iterator end) {
	std::vector<Result>::const_iterator itr = begin;
	while (itr != end) {
		printResult(os, itr);
		itr++;
	}
}

void removeResultIf(std::vector<Result>& results, size_t grade) {

	std::vector<Result>::iterator itr = results.begin();

	while (itr != results.end()) {
		if (itr->grade == grade) {
			itr = results.erase(itr);
		} else {
			itr++;
		}
	}
}

std::vector<Result>::const_iterator findResult(
		std::vector<Result>::const_iterator begin,
		std::vector<Result>::const_iterator end, std::string const& studentId) {
	std::vector<Result>::const_iterator itr = begin;
	while (itr != end) {
		if (itr->studentId.compare(studentId) == 0) {
			return itr;
		} else {
			itr++;
		}
	}

	return itr;

}

void copyResults(std::vector<Result>::const_iterator begin,
		std::vector<Result>::const_iterator end,
		std::back_insert_iterator<std::vector<Result>> insertIt) {
	std::vector<Result>::const_iterator itr = begin;
	while (itr != end) {
		insertIt = (*itr);
		itr++;
	}
}

